var scrollBarWasSetting = false;
var scrollTime = 0;

function init() {
		scrollBarWasSetting = false;
		scheduler.config.multi_day = true;
		scheduler.config.event_duration = 35;
		scheduler.config.xml_date = "%Y-%m-%d %H:%i";

		scheduler.attachEvent(
						"onTemplatesReady",
						function() {
							var lightbox_form = scheduler.getLightbox(); // this
																			// will
																			// generate
																			// lightbox
																			// form
							var inputs = lightbox_form.getElementsByTagName('input');
							var date_of_end = null;
							for ( var i = 0; i < inputs.length; i++) {
								if (inputs[i].name == "date_of_end") {
									date_of_end = inputs[i];
									break;
								}
							}

							var repeat_end_date_format = scheduler.date.date_to_str("%d.%m.%Y");
							var show_minical = function() {
								if (scheduler.isCalendarVisible())
									scheduler.destroyCalendar();
								else {
									scheduler.renderCalendar({
												position : date_of_end,
												date : scheduler._date,
												navigation : true,
												handler : function(date,
														calendar) {
													date_of_end.value = repeat_end_date_format(date);
													scheduler.destroyCalendar()
												}
											});
								}
							};
							date_of_end.onclick = show_minical;

						});

		
		scheduler.config.lightbox.sections = [ {
			name : "subject",
			height : 50,
			map_to : "text",
			type : "textarea",
			focus : true
		},{ 
			name:"environment",
			height:43,
			map_to:"environment",
			type:"textarea"
			},{
			name : "recurring",
			type : "recurring",
			map_to : "rec_type",
			button : "recurring"
		}, {
			name : "time",
			height : 72,
			type : "calendar_time",
			map_to : "auto"
		} ];
		
		if(getWhereAmI()==1) {
			scheduler.init('scheduler_planning', new Date(2013, 0, 14), "week");
		} else if(getWhereAmI()==2) {
			var startDate=$('#resStartDayPeriod').val();
			var date=new Date();
			if(startDate!=""){
				var res = startDate.split("/");
				var day=parseInt(res[0]);
				var month=parseInt(res[1])-1;
				var year=parseInt(res[2]);
				date=new Date(year,month,day);
			}			
			scheduler.init('scheduler_reservation', date, "week");
		} else if(getWhereAmI()==5) {
			scheduler.init('scheduler_reserv_by_env', new Date(), "week");
		}
	}

function setScrollBarPosition(time){
	if(!scrollBarWasSetting){
		scrollBarWasSetting = true;
		if(time != null){
			var realTime = 0;
			if(time[0] == '0'){
				realTime = parseInt(time[1]);
			}else{
				realTime = parseInt(time);
			}
			if(realTime >= 7 && realTime < 11){
				scrollTime = 0;
			}else if(realTime >= 11 && realTime < 15){
				scrollTime = 320;
			}else if(realTime >= 15 && realTime < 19){
				scrollTime = 650;
			}else if(realTime >= 19 && realTime < 23){
				scrollTime = 1050;
			}
		}
		$(".dhx_cal_data").scrollTop(scrollTime);
	}
}

function pInit() {
	init();
}